package com.thinkgem.jeesite.modules.magnetic.util;

import com.thinkgem.jeesite.modules.area_info.entity.AreaInfo;
import com.thinkgem.jeesite.modules.area_info.service.AreaInfoService;
import com.thinkgem.jeesite.modules.area_info_his.entity.AreaInfoHis;
import com.thinkgem.jeesite.modules.area_info_his.service.AreaInfoHisService;
import com.thinkgem.jeesite.modules.area_info_yesterday.entity.AreaInfoYesterday;
import com.thinkgem.jeesite.modules.area_info_yesterday.service.AreaInfoYesterdayService;
import com.thinkgem.jeesite.modules.lane.entity.Lane;
import com.thinkgem.jeesite.modules.lane.service.LaneService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Lazy;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.*;

/**
 * @author xlp
 * @date 2019/4/12
 * 定时任务
 */
@Service
@Lazy(false)
public class TimerUtil {

    @Autowired
    private AreaInfoService areaInfoService;

    @Autowired
    private AreaInfoYesterdayService yesterdayService;

    @Autowired
    private AreaInfoHisService hisService;

    @Autowired
    private LaneService laneService;

    /**
     * 该定时任务: 每天凌晨一点执行一次
     * 任务: 1.将表"t_area_info_yesterday"中的数据按照每小时划分并计算
     *        每个小时内的每个区域的车位占用率
     *      2.将计算并划分好的数据插入表"t_area_info_his"中
     */
    @Scheduled(cron = "0 0 1 * * ?")
    public void task(){
        List<AreaInfo> areaInfos = areaInfoService.findList(new AreaInfo());
        for (AreaInfo areaInfo:areaInfos){
            System.out.println("进行区域"+areaInfo.getAreaName()+"分析");
            //按区域查询当天的数据集合
            List<AreaInfoYesterday> list = yesterdayService.findDayList(areaInfo);
            if (!CollectionUtils.isEmpty(list)){
                DataFlushUtil.handelAreaCarUse(list, hisService);
            }
        }
    }

    /**
     * 定时计划: 每分钟执行一次
     * 任务: 模拟地磁传感器的变化,使车道的剩余车位数发生变化,并把每次发生变化的数据
     *      保存到"t_area_yesterday"中
     */
    public void taskOne(){
        //获取区域
        List<AreaInfo> areaInfoList = areaInfoService.findList(new AreaInfo());
        if (!CollectionUtils.isEmpty(areaInfoList)){
            for (AreaInfo areaInfo:areaInfoList){
                //依次查询每个区域下的车道
                Lane lane = new Lane();
                lane.setAreaId(areaInfo.getId());
                List<Lane> laneList = laneService.findList(lane);
                if (!CollectionUtils.isEmpty(laneList)){
                    for (Lane laneNew:laneList){
                        String totalSpace = laneNew.getTotalSpace();
                        //随机更改该车道的剩余车位数,但不能大于总车位数
                        int num = (int)(Math.random()*Integer.valueOf(totalSpace));
                        laneNew.setRemainSpace(String.valueOf(num));
                        //更新时间
                        laneNew.setUpdateTime(new Date());
                        laneService.save(laneNew);
                    }
                    //随机刷新每个车道的数据后,将刷新后的数据同步到表"t_area_info_yesterday"中
                    AreaInfoYesterday areaInfoYesterday = new AreaInfoYesterday();

                    //获取该区域下的所有车道的剩余车位总和
                    areaInfoYesterday = laneService.getTotalRemainSpace(areaInfo.getId());
                    areaInfoYesterday.setAreaId(areaInfo.getId());
                    areaInfoYesterday.setAreaName(areaInfo.getAreaName());
                    yesterdayService.save(areaInfoYesterday);
                }
            }
        }
    }
}
